<?php
session_start();
/*微博接口*/
include_once 'tools.php';

class weibo
{
    private $appkey = '4083561416';
    private $appSecret = '05355d0adeee67d702e151fb660e3aa6';
    //获取code地址
    private $getCodeUrl = 'https://api.weibo.com/oauth2/authorize';
    //获取token地址
    private $getTokenUrl = 'https://api.weibo.com/oauth2/access_token';
    //回调地址
    private $backUrl = 'http://149818rt73.imwork.net/code.php';
    //拉取数据url
    private $getPublicWeboUrl = 'https://api.weibo.com/2/statuses/home_timeline.json';


    /**
     * 获取code
     */
    public function getCode()
    {
        $data = array(
            'client_id' => $this->appkey,
            'redirect_uri' => $this->backUrl
        );
        $dataString = http_build_query($data);
        $url = $this->httpQuery($this->getCodeUrl, $data);
        header("Location:" . $url);
    }

    /**
     * 从微博获取token
     * @param $code
     */
    public function getWeiboToken($code)
    {
        $data = array(
            'client_id' => $this->appkey,
            'client_secret' => $this->appSecret,
            'grant_type' => 'authorization_code',
            'code' => $code,
            'redirect_uri' => $this->backUrl
        );
        $url = $this->httpQuery($this->getTokenUrl, $data);
        $token = HttpCurl::post($url, '');
        //将token放入session中
        $_SESSION['token'] = $token;
        //跳转授权之前的页面
        $prevPage = $_SESSION['prevPage'] ? $_SESSION['prevPage'] : 'index.php';
        unset($_SESSION['prevPage']);
        header("Location:" . $prevPage);
    }


    /**
     *  获取当前登录用户及其所关注（授权）用户的最新微博
     * @param $page 页数
     * @param $len 数量
     * @return mixed
     */
    public function getPublicWeibo($page = 1, $len = 20)
    {
        $data = array(
            'access_token' => $this->getToken(),
            'page' => $page,
            'count' => $len
        );
        $url = $this->httpQuery($this->getPublicWeboUrl, $data);
        return HttpCurl::get($url, 'json');
    }


    /**
     * 组合请求的url和参数
     * @param $url 请求的url
     * @param $dataArr 参数组成的数组
     * @return string 返回url
     */
    private function httpQuery($url, $dataArr)
    {
        $data = http_build_query($dataArr);
        $url = $url . '?' . $data;
        return $url;
    }


    /**
     * 获取token
     */
    private function getToken()
    {
        $token = $_SESSION['token'];
        if (!empty($token)) {
            $tokenIsTrue = $this->checkToken($token);
            if ($tokenIsTrue) {
                $token = json_decode($token, true);
                return $token['access_token'];
            }
            $this->getCode();
        }
        $this->getCode();
    }


    /**
     * 检测token是否过期
     * @param $token
     */
    private function checkToken($token)
    {
        $tokenData = json_decode($token, true);
        if ($tokenData['expires_in'] - time() > 3600) {
            return false;
        } else {
            return true;
        }
    }

}


